//
//  Add_Manual_CompanyViewController.m
//  HuaRun2
//
//  Created by admin on 17/9/7.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Add_Manual_CompanyViewController.h"
#import "LabelTextfieldView.h"
#import "CompanyViewController.h"
#import "CompanyModel.h"
#import "CRCGroupFlagView.h"
#import "LineView.h"

@interface Add_Manual_CompanyViewController ()

@property(nonatomic, strong)UIScrollView * scrollView;
@property(nonatomic, strong)CompanyModel * companyModel;
//公司英文
@property(nonatomic, strong)LabelTextfieldView * companyEngLF;
//公司中文
@property(nonatomic, strong)LabelTextfieldView * companyChiLF;
//母公司
@property(nonatomic, strong)UIView * companyMotherView;
@property(nonatomic, strong)UILabel * companyMotherNameLab;

//@property(nonatomic, strong)LabelTextfieldView * companyMotherLF;
//CRC Group
@property(nonatomic, strong)CRCGroupFlagView * crcFlagView;
//电话
@property(nonatomic, strong)LabelTextfieldView * phoneLF;
//传真
@property(nonatomic, strong)LabelTextfieldView * chuanZhenLF;
//电邮
@property(nonatomic, strong)LabelTextfieldView * emialLF;
//网址
@property(nonatomic, strong)LabelTextfieldView * netLF;
//线1
@property (nonatomic, strong) LineView * line1;
//线2
@property (nonatomic, strong) LineView * line2;

@end

@implementation Add_Manual_CompanyViewController

//@interface AddCompanyViewController ()<UITextFieldDelegate>
//@end

//@implementation AddCompanyViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    _scrollView.contentSize = CGSizeMake(kMainScreenWidth, CGRectGetMaxY(self.netLF.frame) + 10);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewInit {
    [super viewInit];
    
    self.navigationItem.title = NSLocalizedString(@"add_company", nil);
    
    weakself(ws);
    [self nbLeftImageName:@"qf_back" taget:self clickBlock:^(id result) {
        [ws dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
    
    [self nbRightImageName:@"save" title:nil taget:self clickBlock:^(id result) {
        NSLog(@"....完成");
        [ws submitMethod];
    }];
    
    self.view.backgroundColor =[UIColor whiteColor];// RGBA(249, 249, 249, 1);
    
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.companyEngLF];
    [self.scrollView addSubview:self.companyChiLF];
    
    self.companyMotherView = [self makeCompanyMotherView];
    [self.scrollView addSubview:self.companyMotherView];
    
    [self.scrollView addSubview:self.crcFlagView];
    [self.scrollView addSubview:self.phoneLF];
    [self.scrollView addSubview:self.chuanZhenLF];
    [self.scrollView addSubview:self.emialLF];
    [self.scrollView addSubview:self.netLF];
    [self.scrollView addSubview:self.line1];
    [self.scrollView addSubview:self.line2];
    
    [self.companyMotherNameLab addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        
        [ws chooseMotherCompany];
    }];
    
}

- (void)layoutInit {
    
    weakself(ws);
    
    [self.companyEngLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.scrollView).offset(35);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.companyChiLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.companyEngLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.companyMotherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.companyChiLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
        make.height.mas_equalTo(ws.companyChiLF);
    }];
    
    [self.crcFlagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.companyEngLF.textField.mas_left).offset(kMargin10 * 2);
        make.top.mas_equalTo(ws.line1.mas_bottom).offset(0);
    }];
    
    [self.phoneLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.crcFlagView.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.chuanZhenLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.phoneLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.emialLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.chuanZhenLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.netLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.emialLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.companyMotherView.mas_bottom).offset(0);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(ws.scrollView).offset(kMargin10);
        make.right.mas_equalTo(ws.scrollView).offset(-kMargin10);
    }];
    
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.crcFlagView.mas_bottom).offset(0);
        make.left.right.height.mas_equalTo(ws.line1);
    }];
}

#pragma mark - method
- (void)submitMethod {
    [self.view endEditing:YES];
    
    /*
     name  公司名称，必需
     parentId  母公司ID
     companyNameChineseC 公司名称(中文)
     crcGroup 是否为华润系，布尔类型
     generalLine 公司电话
     faxC     公司传真
     email    电邮
     website  网址
     */
    
    BOOL isCan = [self requireInputMethod];
    if (!isCan) {
        return;
    }
    
    BOOL isPhone = [self optionalPhoneInputMethod];
    if (!isPhone) {
        return;
    }
    BOOL isEmail = [self optionalEmailInputMethod];
    if (!isEmail) {
        return;
    }
    
    NSString * crcGroup = self.crcFlagView.crcFlagView.isSelected ? @"1" : @"0";
    //    return;
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kAddCompanyUrl];
    NSString * name = [NSString showStringWithStr:_companyEngLF.textField.text];
    NSString * companyNameChineseC = [NSString showStringWithStr:_companyChiLF.textField.text];
    NSString * parentId = [NSString showStringWithStr:_companyModel.parentId];
    NSString * generalLine = [NSString showStringWithStr:_phoneLF.textField.text];
    NSString * faxC = [NSString showStringWithStr:_chuanZhenLF.textField.text];
    NSString * email = [NSString showStringWithStr:_emialLF.textField.text];
    NSString * website = [NSString showStringWithStr:_netLF.textField.text];
    
    NSDictionary * dic = @{@"name": name,
                           @"companyNameChineseC": companyNameChineseC,
                           @"parentId": parentId,
                           @"generalLine": generalLine,
                           @"faxC": faxC,
                           @"email": email,
                           @"website": website,
                           @"crcGroup": crcGroup
                           };
    weakself(ws);
//    return;
    [self showActivity:NSLocalizedString(@"正在提交", nil)];

    [NetWorkHelper postWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        [ws showAlert:kAddSuccess completionBlock:^{
            [ws.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
    
}

//必填
- (BOOL)requireInputMethod {
    NSString * text = _companyEngLF.textField.text ;
    if (text.length <= 0) {
        [_companyEngLF showInfoStrInLabelIsEmpty:YES                                            text:NSLocalizedString(@"company_name_error", nil)];
    }
    _companyEngLF.infoLab.hidden = text.length >0;
    
    return text.length > 0;
}

//可选输入
- (BOOL)optionalPhoneInputMethod {
    NSString * text = _phoneLF.textField.text ;
    if (text.length > 0) {
        if ( ![text canUsePhoneNum]) {
            [_phoneLF showInfoStrInLabelIsEmpty:NO text:@""];
            return NO;
        }
    }
    _phoneLF.infoLab.hidden = YES;
    return YES;
}
//可选邮箱输入
- (BOOL)optionalEmailInputMethod {
    NSString * text = _emialLF.textField.text ;
    if (text.length > 0) {
        if ( ![text canUseEmail]) {
            [_emialLF showInfoStrInLabelIsEmpty:NO text:@""];
            return NO;
        }
    }
    _emialLF.infoLab.hidden = YES;
    return YES;
}

- (void)resetEmailInfoLabelMethod {
    _emialLF.infoLab.hidden = YES;
}
//选择母公司
-(void)chooseMotherCompany {
    CompanyViewController * companyVC = [CompanyViewController new];
    companyVC.companyType = CompanyTypeMother;
    weakself(ws);
    companyVC.clickBlock = ^(id result) {
        CompanyModel * companyModel = (CompanyModel *)result;
        //        ws.companyMotherLF.textField.text = companyModel.parentName;
        ws.companyMotherNameLab.text = companyModel.name;
        ws.companyModel = companyModel;
    };
    [self.navigationController pushViewController:companyVC animated:YES];
}

//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//
//{
//
//    if (textField == _companyMotherLF.textField)
//
//    {
//        [self chooseMotherCompany];
//
//    }
//    return YES;
//}
//
//- (void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    [_companyMotherLF.textField resignFirstResponder];
////    [self resignTextResponder];
//
//}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)resetFirstNameInfoLabelMethod {
    _companyEngLF.infoLab.hidden = YES;
}

#pragma mark - getter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

// "company_name_e"= "公司名称(英)";
// "company_name_error"= "公司名称不能为空";
- (LabelTextfieldView *)companyEngLF {
    if (!_companyEngLF) {
        self.companyEngLF = [LabelTextfieldView new];
        _companyEngLF.nameLab.text = NSLocalizedString(@"company_name_e", nil);
        _companyEngLF.starLab.hidden = NO;
//        [_companyEngLF.textField addTarget:self
//                                    action:@selector(requireInputMethod)
//                          forControlEvents:UIControlEventEditingChanged];
        [_companyEngLF.textField addTarget:self
                                      action:@selector(resetFirstNameInfoLabelMethod )
                            forControlEvents:UIControlEventEditingDidBegin];
        
    }
    return _companyEngLF;
}

// "company_name_c"= "公司名称(中)";
- (LabelTextfieldView *)companyChiLF {
    if (!_companyChiLF) {
        self.companyChiLF = [LabelTextfieldView new];
        _companyChiLF.nameLab.text =NSLocalizedString(@"company_name_c", nil);
//        @"公司名称(中)";
    }
    return _companyChiLF;
}

//- (LabelTextfieldView *)companyMotherLF {
//    if (!_companyMotherLF) {
//        self.companyMotherLF = [LabelTextfieldView new];
//        _companyMotherLF.nameLab.text = @"母公司名称";
////        _companyMotherLF.textField.enabled = NO;
//        _companyMotherLF.textField.delegate = self;
////        weakself(ws);
////        [_companyMotherLF.textField addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
////            [ws chooseMotherCompany];
////        }];
//    }
//    return _companyMotherLF;
//}

- (UIView *)makeCompanyMotherView {
    UIView * view = [UIView new];
    
    UILabel * nameLab = [UILabel new];
    nameLab.textAlignment = NSTextAlignmentRight;
    nameLab.font = kGeneralFontSize;
    nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    // "parent_company_name"= "母公司名称";
    nameLab.text = NSLocalizedString(@"parent_company_name", nil);
//    @"母公司名称";
    [view addSubview:nameLab];
    
    UILabel * infoLab = [UILabel new];
    infoLab.textAlignment = NSTextAlignmentLeft;
    infoLab.font = kGeneralFontSize;
    infoLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//    infoLab.layer.borderColor = RGBA(231, 233, 234, 1).CGColor;
//    infoLab.layer.borderWidth = 1;
//    infoLab.layer.masksToBounds = YES;
//    infoLab.layer.cornerRadius = 10;
    [view addSubview:infoLab];
    
    nameLab.backgroundColor = kRandomColor;
    infoLab.backgroundColor = kRandomColor;
    
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:@"return-拷贝"];
    [view addSubview:iconView];
    
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.mas_equalTo(view);
        make.right.mas_equalTo(infoLab.mas_left).offset(-kMargin15);
    }];
    
    [infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(view).offset(-kMargin15);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(view).multipliedBy(0.6);
    }];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(view).offset(-kMargin15);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(10);
    }];
    
    self.companyMotherNameLab = infoLab;
    
//        view.backgroundColor = kRandomColor;
    //    nameLab.backgroundColor = kRandomColor;
    //    infoLab.backgroundColor = kRandomColor;
    
    return view;
}

// "telephone"= "电话";
- (LabelTextfieldView *)phoneLF {
    if (!_phoneLF) {
        self.phoneLF = [LabelTextfieldView new];
        _phoneLF.nameLab.text = NSLocalizedString(@"telephone", nil);
//        @"电话";
        _phoneLF.textField.keyboardType =UIKeyboardTypePhonePad;
//        [_phoneLF.textField addTarget:self
//                               action:@selector(optionalPhoneInputMethod)
//                     forControlEvents:UIControlEventEditingChanged];
        
    }
    return _phoneLF;
}
// "max"= "传真";
- (LabelTextfieldView *)chuanZhenLF {
    if (!_chuanZhenLF) {
        self.chuanZhenLF = [LabelTextfieldView new];
        _chuanZhenLF.nameLab.text = NSLocalizedString(@"max", nil);
        _chuanZhenLF.textField.keyboardType =UIKeyboardTypePhonePad;
        // @"传真";
    }
    return _chuanZhenLF;
}

//"mail"= "电邮";
- (LabelTextfieldView *)emialLF {
    if (!_emialLF) {
        self.emialLF = [LabelTextfieldView new];
        _emialLF.nameLab.text = NSLocalizedString(@"mail", nil);
        _emialLF.textField.keyboardType =UIKeyboardTypeEmailAddress;
//        @"电邮";
        [_emialLF.textField addTarget:self
                               action:@selector(resetEmailInfoLabelMethod)
                     forControlEvents:UIControlEventEditingDidBegin];
        
    }
    return _emialLF;
}

//"url"= "网址";
- (LabelTextfieldView *)netLF {
    if (!_netLF) {
        self.netLF = [LabelTextfieldView new];
        _netLF.nameLab.text = NSLocalizedString(@"url", nil);
// @"网址";
        _netLF.textField.keyboardType =UIKeyboardTypeURL;
        
    }
    return _netLF;
}

- (CRCGroupFlagView *)crcFlagView {
    if (!_crcFlagView) {
        self.crcFlagView = [CRCGroupFlagView new];
        [_crcFlagView.crcFlagView addTarget:self
                                     action:@selector(changeFlagViewStatus:)
                           forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _crcFlagView;
}
- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}

- (LineView *)line2 {
    if (!_line2) {
        self.line2 = [LineView new];
    }
    return _line2;
}

- (void)changeFlagViewStatus:(UIButton *)btn {
    btn.selected = !btn.selected;
}
@end

